package thread;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.Random;

public class work1 {
    public static int[] rand(int[] array) {
        Random random = new Random();
        int MAX = 100,MIN = 1;

        for(int i = 0; i < 10000000; i++) {
            array[i] = random.nextInt(MAX - MIN + 1) + MIN;
        }
//        for(int i = 0; i < 10000000; i++) {
//            System.out.println(array[i]);
//        }
        return array;
    }
    public static int sum1 = 0;
    public static int sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        int[] array = new int[10000000];
        rand(array);

        Thread t1 = new Thread(()-> {
            for(int i = 0; i < 10000000; i++) {
                if(i % 2 == 0) {
                    sum1 += array[i];
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        });

        Date s1 = new Date();
        Date e1 = new Date();
        Date s2 = new Date();
        Date e2 = new Date ();

        System.out.println("t1执行时间:" + (s1.getTime() - e1.getTime()) + "毫秒");

        Thread t2 = new Thread(()-> {

            for(int i = 0; i < 10000000; i++) {
                if(i % 2 != 0) {
                    sum2 += array[i];
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();
        System.out.println("t2执行时间:" + (s2.getTime() - e2.getTime()) + "毫秒");
        System.out.println("sum= "+ sum1+sum2);
    }
  }

